<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python documentation Index' />
<link rel="first" href="lib.html" title='Python library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="node317.html" />
<link rel="prev" href="node315.html" />
<link rel="parent" href="module-pickle.html" />
<link rel="next" href="node317.html" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='aesop' content='information' />
<title>13.1.3 Usage</title>
</head>
<body>
<div class="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="13.1.2 data stream format"
  href="node315.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="13.1 pickle  "
  href="module-pickle.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="13.1.4 what can be"
  href="node317.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="node315.html">13.1.2 Data stream format</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-pickle.html">13.1 pickle  </a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="node317.html">13.1.4 What can be</a>
</div>
<hr /></div>
</div>
<!--End of Navigation Panel-->

<h2><a name="SECTION0015130000000000000000">
13.1.3 Usage</a>
</h2>

<p>
To serialize an object hierarchy, you first create a pickler, then you
call the pickler's <tt class="method">dump()</tt> method.  To de-serialize a data
stream, you first create an unpickler, then you call the unpickler's
<tt class="method">load()</tt> method.  The <tt class="module">pickle</tt> module provides the
following constant:

<p>
<dl><dt><b><tt id='l2h-2454' xml:id='l2h-2454'>HIGHEST_PROTOCOL</tt></b></dt>
<dd>
The highest protocol version available.  This value can be passed
as a <var>protocol</var> value.

<span class="versionnote">New in version 2.3.</span>

</dd></dl>

<p>
<span class="note"><b class="label">Note:</b>
Be sure to always open pickle files created with protocols &gt;= 1 in
      binary mode. For the old ASCII-based pickle protocol 0 you can use
      either text mode or binary mode as long as you stay consistent.

<p>
A pickle file written with protocol 0 in binary mode will contain
      lone linefeeds as line terminators and therefore will look ``funny''
      when viewed in Notepad or other editors which do not support this
      format.</span>

<p>
The <tt class="module">pickle</tt> module provides the
following functions to make the pickling process more convenient:

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2455' xml:id='l2h-2455' class="function">dump</tt></b>(</nobr></td>
  <td><var>obj, file</var><big>[</big><var>, protocol</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Write a pickled representation of <var>obj</var> to the open file object
<var>file</var>.  This is equivalent to
<code>Pickler(<var>file</var>, <var>protocol</var>).dump(<var>obj</var>)</code>.

<p>
If the <var>protocol</var> parameter is omitted, protocol 0 is used.
If <var>protocol</var> is specified as a negative value
or <tt class="constant">HIGHEST_PROTOCOL</tt>,
the highest protocol version will be used.

<p>

<span class="versionnote">Changed in version 2.3:
Introduced the <var>protocol</var> parameter.</span>

<p>
<var>file</var> must have a <tt class="method">write()</tt> method that accepts a single
string argument.  It can thus be a file object opened for writing, a
<tt class="module"><a href="module-StringIO.html">StringIO</a></tt> object, or any other custom
object that meets this interface.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2456' xml:id='l2h-2456' class="function">load</tt></b>(</nobr></td>
  <td><var>file</var>)</td></tr></table></dt>
<dd>
Read a string from the open file object <var>file</var> and interpret it as
a pickle data stream, reconstructing and returning the original object
hierarchy.  This is equivalent to <code>Unpickler(<var>file</var>).load()</code>.

<p>
<var>file</var> must have two methods, a <tt class="method">read()</tt> method that takes
an integer argument, and a <tt class="method">readline()</tt> method that requires no
arguments.  Both methods should return a string.  Thus <var>file</var> can
be a file object opened for reading, a
<tt class="module">StringIO</tt> object, or any other custom
object that meets this interface.

<p>
This function automatically determines whether the data stream was
written in binary mode or not.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2457' xml:id='l2h-2457' class="function">dumps</tt></b>(</nobr></td>
  <td><var>obj</var><big>[</big><var>, protocol</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Return the pickled representation of the object as a string, instead
of writing it to a file.

<p>
If the <var>protocol</var> parameter is omitted, protocol 0 is used.
If <var>protocol</var> is specified as a negative value
or <tt class="constant">HIGHEST_PROTOCOL</tt>,
the highest protocol version will be used.

<p>

<span class="versionnote">Changed in version 2.3:
The <var>protocol</var> parameter was added.</span>

<p>
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2458' xml:id='l2h-2458' class="function">loads</tt></b>(</nobr></td>
  <td><var>string</var>)</td></tr></table></dt>
<dd>
Read a pickled object hierarchy from a string.  Characters in the
string past the pickled object's representation are ignored.
</dl>

<p>
The <tt class="module">pickle</tt> module also defines three exceptions:

<p>
<dl><dt><b><span class="typelabel">exception</span>&nbsp;<tt id='l2h-2459' xml:id='l2h-2459' class="exception">PickleError</tt></b></dt>
<dd>
A common base class for the other exceptions defined below.  This
inherits from <tt class="exception">Exception</tt>.
</dd></dl>

<p>
<dl><dt><b><span class="typelabel">exception</span>&nbsp;<tt id='l2h-2460' xml:id='l2h-2460' class="exception">PicklingError</tt></b></dt>
<dd>
This exception is raised when an unpicklable object is passed to
the <tt class="method">dump()</tt> method.
</dd></dl>

<p>
<dl><dt><b><span class="typelabel">exception</span>&nbsp;<tt id='l2h-2461' xml:id='l2h-2461' class="exception">UnpicklingError</tt></b></dt>
<dd>
This exception is raised when there is a problem unpickling an object.
Note that other exceptions may also be raised during unpickling,
including (but not necessarily limited to) <tt class="exception">AttributeError</tt>,
<tt class="exception">EOFError</tt>, <tt class="exception">ImportError</tt>, and <tt class="exception">IndexError</tt>.
</dd></dl>

<p>
The <tt class="module">pickle</tt> module also exports two callables<a name="tex2html110"
  href="#foot36738"><sup>13.2</sup></a>, <tt class="class">Pickler</tt> and <tt class="class">Unpickler</tt>:

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-2462' xml:id='l2h-2462' class="class">Pickler</tt></b>(</nobr></td>
  <td><var>file</var><big>[</big><var>, protocol</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
This takes a file-like object to which it will write a pickle data
stream.  

<p>
If the <var>protocol</var> parameter is omitted, protocol 0 is used.
If <var>protocol</var> is specified as a negative value,
the highest protocol version will be used.

<p>

<span class="versionnote">Changed in version 2.3:
Introduced the <var>protocol</var> parameter.</span>

<p>
<var>file</var> must have a <tt class="method">write()</tt> method that accepts a single
string argument.  It can thus be an open file object, a
<tt class="module">StringIO</tt> object, or any other custom
object that meets this interface.
</dl>

<p>
<tt class="class">Pickler</tt> objects define one (or two) public methods:

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2463' xml:id='l2h-2463' class="method">dump</tt></b>(</nobr></td>
  <td><var>obj</var>)</td></tr></table></dt>
<dd>
Write a pickled representation of <var>obj</var> to the open file object
given in the constructor.  Either the binary or ASCII format will
be used, depending on the value of the <var>protocol</var> argument passed to the
constructor.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2464' xml:id='l2h-2464' class="method">clear_memo</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
Clears the pickler's ``memo''.  The memo is the data structure that
remembers which objects the pickler has already seen, so that shared
or recursive objects pickled by reference and not by value.  This
method is useful when re-using picklers.

<p>
<div class="note"><b class="label">Note:</b>
Prior to Python 2.3, <tt class="method">clear_memo()</tt> was only available on the
picklers created by <tt class="module"><a href="module-cPickle.html">cPickle</a></tt>.  In the <tt class="module">pickle</tt> module,
picklers have an instance variable called <tt class="member">memo</tt> which is a
Python dictionary.  So to clear the memo for a <tt class="module">pickle</tt> module
pickler, you could do the following:

<p>
<div class="verbatim"><pre>
mypickler.memo.clear()
</pre></div>

<p>
Code that does not need to support older versions of Python should
simply use <tt class="method">clear_memo()</tt>.
</div>
</dl>

<p>
It is possible to make multiple calls to the <tt class="method">dump()</tt> method of
the same <tt class="class">Pickler</tt> instance.  These must then be matched to the
same number of calls to the <tt class="method">load()</tt> method of the
corresponding <tt class="class">Unpickler</tt> instance.  If the same object is
pickled by multiple <tt class="method">dump()</tt> calls, the <tt class="method">load()</tt> will
all yield references to the same object.<a name="tex2html111"
  href="#foot36740"><sup>13.3</sup></a>
<p>
<tt class="class">Unpickler</tt> objects are defined as:

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-2465' xml:id='l2h-2465' class="class">Unpickler</tt></b>(</nobr></td>
  <td><var>file</var>)</td></tr></table></dt>
<dd>
This takes a file-like object from which it will read a pickle data
stream.  This class automatically determines whether the data stream
was written in binary mode or not, so it does not need a flag as in
the <tt class="class">Pickler</tt> factory.

<p>
<var>file</var> must have two methods, a <tt class="method">read()</tt> method that takes
an integer argument, and a <tt class="method">readline()</tt> method that requires no
arguments.  Both methods should return a string.  Thus <var>file</var> can
be a file object opened for reading, a
<tt class="module">StringIO</tt> object, or any other custom
object that meets this interface.
</dl>

<p>
<tt class="class">Unpickler</tt> objects have one (or two) public methods:

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2466' xml:id='l2h-2466' class="method">load</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
Read a pickled object representation from the open file object given
in the constructor, and return the reconstituted object hierarchy
specified therein.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2467' xml:id='l2h-2467' class="method">noload</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
This is just like <tt class="method">load()</tt> except that it doesn't actually
create any objects.  This is useful primarily for finding what's
called ``persistent ids'' that may be referenced in a pickle data
stream.  See section&nbsp;<a href="pickle-protocol.html#pickle-protocol">13.1.5</a> below for more details.

<p>
<strong>Note:</strong> the <tt class="method">noload()</tt> method is currently only
available on <tt class="class">Unpickler</tt> objects created with the
<tt class="module">cPickle</tt> module.  <tt class="module">pickle</tt> module <tt class="class">Unpickler</tt>s do
not have the <tt class="method">noload()</tt> method.
</dl>

<p>
<br><hr><h4>Footnotes</h4>
<dl>
<dt><a name="foot36738">... callables</a><A
 HREF="node316.html#tex2html110"><sup>13.2</sup></a></dt>
<dd>In the
<tt class="module">pickle</tt> module these callables are classes, which you could
subclass to customize the behavior.  However, in the <tt class="module"><a href="module-cPickle.html">cPickle</a></tt>
module these callables are factory functions and so cannot be
subclassed.  One common reason to subclass is to control what
objects can actually be unpickled.  See section&nbsp;<a href="pickle-sub.html#pickle-sub">13.1.6</a> for
more details.

</dd>
<dt><a name="foot36740">... object.</a><A
 HREF="node316.html#tex2html111"><sup>13.3</sup></a></dt>
<dd><em>Warning</em>: this
is intended for pickling multiple objects without intervening
modifications to the objects or their parts.  If you modify an object
and then pickle it again using the same <tt class="class">Pickler</tt> instance, the
object is not pickled again -- a reference to it is pickled and the
<tt class="class">Unpickler</tt> will return the old value, not the modified one.
There are two problems here: (1) detecting changes, and (2)
marshalling a minimal set of changes.  Garbage Collection may also
become a problem here.

</dd>
</dl>
<div class="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="13.1.2 data stream format"
  href="node315.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="13.1 pickle  "
  href="module-pickle.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="13.1.4 what can be"
  href="node317.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="node315.html">13.1.2 Data stream format</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-pickle.html">13.1 pickle  </a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="node317.html">13.1.4 What can be</a>
</div>
</div>
<hr />
<span class="release-info">Release 2.5.1, documentation updated on 18th April, 2007.</span>
</div>
<!--End of Navigation Panel-->
<address>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</address>
</body>
</html>
